/**
 * 功能: sessionStorage 功能封装
 * @author Conan
 * @CreatedTime 2020-07-24
 */

const uzStorage = function() {
  return window.sessionStorage
}

class SStorage {
  // 设置缓存
  static setStorage(key, value) {
    var v = value
    if (typeof v === 'object') {
      v = JSON.stringify(v)
      v = 'obj-' + v
    } else {
      v = 'str-' + v
    }
    var ls = uzStorage()
    if (ls) {
      ls.setItem(key, v)
    }
  }
  // 获取缓存
  static getStorage(key) {
    var ls = uzStorage()
    if (ls) {
      var v = ls.getItem(key)
      if (!v) {
        return false
      }
      if (v.indexOf('obj-') === 0) {
        v = v.slice(4)
        return JSON.parse(v)
      } else if (v.indexOf('str-') === 0) {
        return v.slice(4)
      }
    }
  }
  // 移除缓存
  static removeStorage(key) {
    if (!key) return false
    var ls = uzStorage()
    ls.removeItem(key)
  }
}
export default SStorage
